//
//  BSXPFViewController.m
//  BaiSiBuDeJie
//
//  Created by XPF on 16/8/24.
//  Copyright © 2016年 tsaievan. All rights reserved.
//

#import "BSXPFViewController.h"
#import "BSXPFCollectionViewCell.h"
#import "BSXPFThroughModel.h"
#import "BSBaseModel.h"

static NSString *BSXPFCollectionCell = @"BSXPFCollectionCell";

@interface BSXPFViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (strong,nonatomic) NSArray *dataArr;

@property (weak,nonatomic) UIView *projectView;

@property (strong,nonatomic) UICollectionView *contentCollectionView;

@property (strong,nonatomic) NSArray  *modelArr;

@end

@implementation BSXPFViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //加载穿越界面
    [self loadThroughView];
    
    //加载中间的项目view
    [self loadProjectView];
    
    //加载下面的内容collectionView
    [self loadContentCollectionView];
    
    [self.contentCollectionView registerClass:[BSXPFCollectionViewCell class] forCellWithReuseIdentifier:BSXPFCollectionCell];
    

}

//设置穿越界面
- (void)loadThroughView{
    
    self.navigationItem.title = @"穿越";
    
    
    //设置右上角的刷新按钮
    UIButton *refreshBtn = [[UIButton alloc] init];
    
    [refreshBtn addTarget:self action:@selector(reloadViewData) forControlEvents:UIControlEventTouchUpInside];
    
    [refreshBtn sizeToFit];
    
    refreshBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -20);
    
//    [refreshBtn setImage:[UIImage imageNamed:@"navigationButtonRandom_26x26_"] forState:UIControlStateNormal];
//    
//    [refreshBtn setImage:[UIImage imageNamed:@"navigationButtonRandomClick_26x26_"] forState:UIControlStateHighlighted];
    
    [refreshBtn setImage:[UIImage imageNamed:@"cellFollowClickIcon"] forState:UIControlStateNormal];
    
    [refreshBtn setImage:[UIImage imageNamed:@"cellFollowClickIcon"] forState:UIControlStateHighlighted];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:refreshBtn];
    
}

- (void)loadProjectView{
    
    //设置中间的有几个按钮的view
    UIView *projectView = [[UIView alloc] init];
    
    self.projectView = projectView;
    
    projectView.frame = CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame) , kScreenW, 44);
    
    CGFloat bW = kScreenW / self.dataArr.count;

    
    for (NSInteger i = 0; i < self.dataArr.count; i++) {
        
        UIButton *btn = [[UIButton alloc] init];
        
        btn.tag = i;
        
        [btn setTitle:self.dataArr[i] forState:UIControlStateNormal];
        
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        btn.frame = CGRectMake(i*bW, 0, bW, 44);
        
        [btn addTarget:self action:@selector(clickProjectBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [projectView addSubview:btn];
        
    }
    
    [self.view addSubview:projectView];
    
}

- (void)loadContentCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    CGFloat contentY = CGRectGetMaxY(self.projectView.frame) + 8;
    
    UICollectionView *contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, contentY, kScreenW,kScreenH - contentY) collectionViewLayout:flowLayout];
    
    self.contentCollectionView = contentCollectionView;
    
    contentCollectionView.backgroundColor = [UIColor whiteColor];
    
    contentCollectionView.delegate = self;
    
    contentCollectionView.dataSource = self;
    
    contentCollectionView.pagingEnabled = YES;
    
    contentCollectionView.showsHorizontalScrollIndicator = NO;
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    flowLayout.minimumLineSpacing = 0;
    
    flowLayout.itemSize = CGSizeMake(kScreenW, kScreenH - contentY);
    [self.view addSubview:contentCollectionView];
    
}

- (void)clickProjectBtn:(UIButton *)sender{
    
    NSLog(@"qiehua");
    
    
}

- (void)reloadViewData{
    
    [self.contentCollectionView reloadData];
    
    NSLog(@"..");
    
}

-(NSArray *)dataArr{
    
    
    if (!_dataArr) {
        
        _dataArr = @[@"全部",@"视频",@"图片",@"段子",@"声音"];
        
    }
    
    return _dataArr;
    
}

#pragma mark - collectionView的代理和数据源
//视频
//http://d.api.budejie.com/topic/list/chuanyue/41/bs0315-iphone-4.3/0-20.json
//http://d.api.budejie.com/topic/list/chuanyue/41/bs0315-iphone-4.3/0-20.json

//段子
//http://d.api.budejie.com/topic/list/chuanyue/29/bs0315-iphone-4.3/0-20.json

//精华
//http://s.budejie.com/topic/list/jingxuan/10/bs0315-iphone-4.3/0-20.json

//http://s.budejie.com/topic/tag-topic/64/hot/bs0315-iphone-4.3/0-20.json
//http://s.budejie.com/topic/tag-topic/3096/hot/bs0315-iphone-4.3/0-20.json
//http://s.budejie.com/topic/list/remen/1/bs0315-iphone-4.3/0-20.json

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    BSXPFCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:BSXPFCollectionCell forIndexPath:indexPath];
    
//    [[DownloadManager sharedDownloadManager] GET:@"http://d.api.budejie.com/topic/list/chuanyue/41/bs0315-iphone-4.3/0-20.json" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        NSDictionary *dic = responseObject;
//        
//        NSArray *arr = dic[@"list"];
//        
//     cell.modelArr =  [BSXPFThroughModel throughModelWithArray:arr];
//        
//        NSLog(@"成功");
//        
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        NSLog(@"%@",error);
//        
//        
//    }];
    
    [BSBaseModel downDataWithUrl:@"http://d.api.budejie.com/topic/list/chuanyue/41/bs0315-iphone-4.3/0-20.json" successBlock:^(BSBaseModel *model) {
        
        cell.model = model;
        
    } failBlock:^(NSError *err) {
        
        
        if (err) {
            NSLog(@"%@",err);
        }
        
    }];
    
     
    
    return cell;
    
    
}


@end
